import time

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

from common.basepage import BasePage


class AppointmentPage(BasePage):
    """预约页面"""
    #  _locator = (By.XPATH, '')
    """预约大屏页面元素"""
    # 新增预约按钮
    newAppointment_btn_locator = (By.XPATH, '//*[@id="newAppointments"]')
    # 锁定时间按钮
    lockTime_btn_locator = (By.XPATH, '//*[@class="lock-time"]')
    # 待分配框-添加预约按钮，随机选择
    assign_appointment_btn = (By.XPATH, '//*[@class="contenter"]/div[2]/div[1]/div[@class="cell"]')
    # 待分配框-修改
    assign_change_locator = (By.XPATH, '//div[@class="appt wait-assign"][1]//ul[1]/li[1]')
    # 待分配框-开单
    assign_billing_locator = (By.XPATH, '//div[@class="appt wait-assign"][1]//ul[1]/li[2]')
    # 待分配框-取消预约
    assign_cancleAppointment_locator = (By.XPATH, '//div[@class="appt wait-assign"][1]//ul[1]/li[3]')
    # 技师框-添加预约按钮，随机选择，div[1]/div[@class="cell"]代表第一个技师的时间框
    technician_appointment_btn = (By.XPATH, '//*[@class="contenter"]/div[2]/div[1]/following-sibling::div[2]/div[@class="cell"]')
    # 技师框-修改，
    technician_change_locator = (By.XPATH, '//div[@class="appt wait-servered"][1]//li[1]')
    # 技师框-开单
    technician_billing_locator = (By.XPATH, '//div[@class="appt wait-servered"][1]//li[2]')
    # 技师框-取消预约
    technician_cancleAppointment_locator = (By.XPATH, '//div[@class="appt wait-servered"][1]//li[3]')


    """新增预约页面元素"""
    # 客人手机号
    guestMobile_input_locator = (By.XPATH,'//*[contains(text(),"到店人手机号")]/following-sibling::input')
    # 客人姓名
    guestName_input__locator = (By.XPATH, '//*[contains(text(),"到店人姓名")]/following-sibling::input')
    # 预约门店
    appointment_shop_btn = (By.XPATH, '//*[contains(text(),"预约门店")]/following-sibling::div')
    # 选中门店，可随机选择一个
    shop_selects_locator = (By.XPATH, '//*[@x-placement="bottom-start"]//li[contains(@class,"el-select-dropdown__item")]')
    # 到店日期
    arrival_date_locator = (By.XPATH, '//*[contains(@aria-label,"预约")]//input[@placeholder="选择日期"]')
    # 选择具体日期，随机选择一个
    arrivalDate_selects_locator = (By.XPATH, '//div[@x-placement="bottom-start"]//*[@class="el-date-table"]//td[contains(@class,"available")]')
    # 到店时间
    arrival_time_locator = (By.CSS_SELECTOR, 'div[aria-label="新增预约"]  input[placeholder="选择时间"]')
    # 选择具体时间，随机选择一个
    arrivalTime_selects_locator = (By.XPATH,'//*[@class="time-select-item"]')
    # 服务项目
    services_btn_locator = (By.XPATH, '//*[contains(text(),"服务项目")]/following-sibling::div')
    # 选择具体服务,随机选择一个
    services_selects_locator = (By.XPATH, '//*[@x-placement="bottom-start"]//li')
    # 服务技师
    technician_btn_locator = (By.XPATH, '//*[contains(text(),"服务技师:")]/following-sibling::input')
    # 选择具体技师,随机选择一个
    technician_selects_locator = (By.XPATH, '//*[@class="select-area"]//label[1]')
    # 跟随技师，选择点客
    customer_checkbox_locator = (By.XPATH, '//*[@class="select-area"]//label[2][@class="el-checkbox"]')
    # 备注
    remarks_input_locator = (By.XPATH, '//*[contains(text(),"备注")]/following-sibling::textarea')
    # 确定按钮
    appointment_submit_locator = (By.XPATH, '//*[@class="dialog-footer"]/button[4]')

    """取消页面元素"""
    # 取消原因按钮
    cancleReason_btn_locator = (By.XPATH, '//*[@aria-label="取消预约"]//div/input')
    # 取消原因选项
    cancleReason_selects_locator = (By.XPATH, '//*[@x-placement="bottom-start"]//li')
    # 取消预约备注
    cancle_remarks_locator = (By.XPATH, '//*[@aria-label="取消预约"]//textarea')
    # 取消预约确定按钮
    cancle_confirm_locator = (By.XPATH, '//*[@aria-label="取消预约"]/div[3]//button[2]')
    # 列表取消预约确定按钮
    cancle_listConfirm_locator = (By.XPATH, '//*[@aria-label="取消预约"]/div[3]//button[3]')

    """修改页面元素"""
    # 修改-确定按钮
    change_confirm_locator = (By.XPATH, '//*[@aria-label="修改预约"]//button[3]')


    """锁定时间页面元素"""
    # _locator = (By.XPATH, '')
    # 锁定日期
    lockDate_btn_locator = (By.XPATH, '//*[@aria-label="锁定时间"]//input[@placeholder="选择日期"]')
    # 选择具体日期，随机选择
    lockDate_selects_locator = (By.XPATH, '//div[@x-placement="bottom-start"]//*[@class="el-date-table"]//td[contains(@class,"available")]')
    # 锁定员工
    lockStaff_btn_locator = (By.XPATH, '//*[contains(text(),"锁定员工")]/following-sibling::div')
    # 选择具体员工，随机选择
    lockStaff_selects_locator = (By.XPATH, '//*[@x-placement="bottom-start"]//li')
    # 开始时间
    lockStart_btn_locator = (By.XPATH, '//*[@class="locked-dialog"]/p/span[contains(text(),"锁定时间")]/following-sibling::div[1]')
    # 选择具体开始时间，随机选择
    lockStart_selects_locator = (By.XPATH, '//*[contains(@x-placement,"start")]//div[@class="time-select-item"]')
    # 结束时间
    lockEnd_btn_locator = (By.XPATH, '//*[@class="locked-dialog"]/p/span[contains(text(),"锁定时间")]/following-sibling::div[2]')
    # 选择具体结束时间，随机选择
    lockEnd_selects_locator = (By.XPATH, '//*[contains(@x-placement,"start")]//div[@class="time-select-item"]')
    # 确定按钮
    lockTime_Submit_locator = (By.XPATH, '//div[@class="el-dialog__body"]/following-sibling::div/span/button[2]')
    # 锁定成功提示信息
    lockTime_prompt_msg = (By.XPATH, '//*[@class="el-message__content"]')


    """预约列表页面元素"""
    hover = (By.XPATH, '//*[@id="content"]/div/div[2]/div[1]/div[99]')
    # 预约列表标签
    appointment_list_locator = (By.XPATH, '//*[text()="预约列表"]')
    # 到店时间选项-全部
    arrivalDate_All_locator = (By.XPATH, '//*[@class="filter-area"]/div[1]/span[text()="全部"]')
    # 预约状态选项-全部
    appointmentStatus_All_locator = (By.XPATH, '//*[@class="filter-area"]/div[2]/span[text()="全部"]')
    # 预约状态选项-待服务
    appointmentStatus_serve_locator = (By.XPATH, '//*[@class="filter-area"]/div[2]/span[text()="待服务"]')
    # 预约状态选项-待分配
    appointmentStatus_assign_locator = (By.XPATH, '//*[@class="filter-area"]/div[2]/span[text()="待分配"]')
    # 预约状态选项-已完成
    appointmentStatus_finished_locator = (By.XPATH, '//*[@class="filter-area"]/div[2]/span[text()="已完成"]')
    # 预约状态选项-已取消
    appointmentStatus_cancel_locator = (By.XPATH, '//*[@class="filter-area"]/div[2]/span[text()="已取消"]')
    # 新增预约按钮
    addAppointment_btn_locator = (By.XPATH, '//*[@class="add-btn"]')
    # 查询预约输入框
    search_input_locator = (By.XPATH, '//*[@class="search-input"]')
    # 查询按钮
    search_btn_locator = (By.XPATH, '//*[@class="icon-search"]')
    # 预约来源下拉选择按钮
    appointmentSource_btn_locator = (By.XPATH, '//*[contains(text(),"预约来源")]/following-sibling::div[1]//input')
    # 预约来源下拉选择项
    appointmentSource_select_locator = (By.XPATH, '//*[@x-placement="bottom-start"]//li[2]')
    # 取消预约按钮
    list_cancleAppointment_locator = (By.XPATH, '//*[@id="app"]//div[4]/div[2]/table/tbody/tr/td[9]/div/div[1]/span[1]')
    # 修改按钮
    list_change_locator = (By.XPATH, '//*[@id="app"]//div[4]/div[2]/table/tbody/tr/td[9]/div/div[1]/span[2]')
    # 开单按钮
    list_billing_locator = (By.XPATH, '//*[@id="app"]//div[4]/div[2]/table/tbody/tr/td[9]/div/div[1]/span[3]')
    # 查看按钮
    list_veiw_locator = (By.XPATH, '//tr/td[@class="el-table_4_column_36  "]/div/div[3]/span')




    """预约大屏功能"""

    """新增预约"""
    def click_newAppointment(self):
        """点击新增预约按钮"""
        self.clickable_wait(self.newAppointment_btn_locator).click()

    def input_guestMobile(self,mobile):
        """输入客户手机号"""
        mobile_elem = self.visible_only_wait(self.guestMobile_input_locator)
        mobile_elem.clear()
        mobile_elem.send_keys(mobile)

    def input_guestName(self,name):
        """输入客户姓名"""
        name_elem = self.visible_only_wait(self.guestName_input__locator)
        name_elem.clear()
        name_elem.send_keys(name)

    def select_appointment_shop(self):
        """选择预约门店，但一般不用，直接默认"""
        self.clickable_wait(self.appointment_shop_btn).click()
        shops = self.visible_least_wait(self.shop_selects_locator)
        self.random_select_click(shops)

    def select_arrivalDate(self):
        """选择到店日期"""
        self.clickable_wait(self.arrival_date_locator).click()
        dates = self.visible_least_wait(self.arrivalDate_selects_locator)
        self.random_JSselect_click(dates)

    def select_arrivalTime(self):
        """选择到店时间"""
        self.presence_only_wait(self.arrival_time_locator).click()
        times = self.presence_least_wait(self.arrivalTime_selects_locator)
        self.random_JSselect_click(times)

    def select_service(self):
        """选择服务项目"""
        self.clickable_wait(self.services_btn_locator).click()
        services = self.presence_least_wait(self.services_selects_locator)
        self.random_JSselect_click(services)

    def select_technician(self):
        """选择服务技师"""
        self.visible_only_wait(self.technician_btn_locator).click()
        technicians = self.presence_least_wait(self.technician_selects_locator)
        self.random_select_click(technicians)

    def select_customer(self):
        """选择点客"""
        self.visible_only_wait(self.customer_checkbox_locator).click()

    def input_remarks(self,remarks="新增预约的备注"):
        """输入备注"""
        self.visible_only_wait(self.remarks_input_locator).send_keys(remarks)

    def click_submit(self):
        """点击确定按钮提交"""
        self.clickable_wait(self.appointment_submit_locator).click()


    def newAppointment(self,mobile,name):
        """新增预约"""

        # 输入到店人手机号
        self.input_guestMobile(mobile)
        # 输入到店人姓名
        self.input_guestName(name)
        # 预约门店,使用默认
        # 选择到店日期
        self.select_arrivalDate()
        # 选择到店时间
        self.select_arrivalTime()
        # 选择服务项目
        self.select_service()
        # 选择服务技师
        self.select_technician()
        # 选择点客
        self.select_customer()
        # 输入备注
        self.input_remarks()
        # 点击确定
        self.click_submit()


    """锁定时间"""

    def lockTime(self):
        """锁定时间"""
        # 点击锁定时间按钮
        self.clickable_wait(self.lockTime_btn_locator).click()
        # 选择锁定日期
        self.clickable_wait(self.lockDate_btn_locator).click()
        dates = self.visible_least_wait(self.lockDate_selects_locator)
        self.random_select_click(dates)
        # 选择锁定员工
        self.clickable_wait(self.lockStaff_btn_locator).click()
        staff = self.visible_least_wait(self.lockStaff_selects_locator)
        self.random_JSselect_click(staff)
        # 选择锁定时间
        # 开始时间
        self.clickable_wait(self.lockStart_btn_locator).click()
        start = self.visible_least_wait(self.lockStart_selects_locator)
        self.random_JSselect_click(start)
        # 结束时间
        # 是根据开始时间自动计算结束时间，需要加强制等待
        self.clickable_wait(self.lockEnd_btn_locator).click()
        end = self.visible_least_wait(self.lockEnd_selects_locator)
        self.order_JSselect_click(end)
        # 点击确定
        self.clickable_wait(self.lockTime_Submit_locator).click()



    """待分配时间框功能"""

    def click_assign_appointment(self):
        """随机点击待分配时间框的添加预约按钮"""
        appointment_btns = self.visible_least_wait(self.assign_appointment_btn)
        self.random_JSselect_click(appointment_btns)

    def click_change_confirm(self):
        """点击修改页面的确定按钮"""
        self.clickable_wait(self.change_confirm_locator).click()

    def assign_appointment(self,mobile,name):
        """待分配时间框-添加预约
        和新增预约的区别是无法选择服务技师
        """
        # 随机点击待分配时间框的添加预约按钮
        self.click_assign_appointment()
        # 输入到店人手机号
        self.input_guestMobile(mobile)
        # 输入到店人姓名
        self.input_guestName(name)
        # 预约门店,使用默认
        # 选择到店日期,使用默认
        # 选择到店时间
        self.select_arrivalTime()
        # 选择服务项目
        self.select_service()
        # 输入备注
        self.input_remarks()
        # 点击确定
        self.click_submit()



    def assign_change(self):
        """待分配时间框-修改预约"""
        # 点击修改按钮
        change_btn = self.presence_only_wait(self.assign_change_locator)
        self.JS_click(change_btn)
        # 修改服务项目
        self.select_service()
        # 点击确定
        self.click_change_confirm()

    def assign_billing(self):
        """待分配时间框-开单"""
        billing_btn = self.presence_only_wait(self.assign_billing_locator)
        self.JS_click(billing_btn)

    def click_assign_cancleAppointment(self):
        """待分配框-点击取消预约"""
        cancleAppointment_btn = self.presence_only_wait(self.assign_cancleAppointment_locator)
        self.JS_click(cancleAppointment_btn)

    """取消预约功能"""
    def cancle_appointment(self):
        """通用取消预约功能"""
        # 点击取消原因
        self.clickable_wait(self.cancleReason_btn_locator).click()
        # 下拉选择取消原因
        reasons = self.presence_least_wait(self.cancleReason_selects_locator)
        self.random_JSselect_click(reasons)
        # 输入取消原因
        self.visible_only_wait(self.cancle_remarks_locator).send_keys("取消原因备注")
        # 点击确定按钮
        self.clickable_wait(self.cancle_confirm_locator).click()

    def cancle_list_appointment(self):
        """通用取消预约功能"""
        # 点击取消原因
        self.clickable_wait(self.cancleReason_btn_locator).click()
        # 下拉选择取消原因
        reasons = self.presence_least_wait(self.cancleReason_selects_locator)
        self.random_JSselect_click(reasons)
        # 输入取消原因
        self.visible_only_wait(self.cancle_remarks_locator).send_keys("取消原因备注")
        # 点击确定按钮
        self.clickable_wait(self.cancle_listConfirm_locator).click()


    """技师时间框功能"""

    def click_technician_appointment(self):
        """点击技师时间框的添加预约按钮"""
        appointment_btns = self.visible_least_wait(self.technician_appointment_btn)
        self.order_JSselect_click(appointment_btns)

    def click_technician_change(self):
        """点击技师时间框的修改按钮"""
        change_btn = self.presence_only_wait(self.technician_change_locator)
        self.JS_click(change_btn)

    def click_technician_billing(self):
        """点击技师时间框的开单按钮"""
        billing_btn = self.presence_only_wait(self.technician_billing_locator)
        self.JS_click(billing_btn)

    def click_technician_cancleAppointment(self):
        """点击技师时间框的取消按钮"""
        cancleAppointment_btn = self.presence_only_wait(self.technician_cancleAppointment_locator)
        self.JS_click(cancleAppointment_btn)

    def technician_appointment(self, mobile, name):
        """技师时间框-添加预约
        和新增预约的区别是时间，技师已选定
        """
        # 随机点击技师时间框的添加预约按钮
        self.click_technician_appointment()
        # 输入到店人手机号
        self.input_guestMobile(mobile)
        # 输入到店人姓名
        self.input_guestName(name)
        # 预约门店,使用默认
        # 选择到店日期,使用默认
        # 选择到店时间
        # self.select_arrivalTime()
        # 选择服务项目
        self.select_service()
        # 输入备注
        self.input_remarks()
        # 点击确定
        self.click_submit()

    def technician_change(self):
        """技师时间框-修改预约"""
        # 点击修改按钮
        self.click_technician_change()
        #
        self.select_arrivalTime()
        # 修改服务项目
        self.select_service()
        # 点击确定
        self.click_change_confirm()

    def get_prompt_msg(self):
        """获取成功提示信息"""
        # 弹出的提示框信息，presence等待方法定位失败
        time.sleep(2)
        return self.presence_only_wait(self.lockTime_prompt_msg)


    """预约列表功能"""

    def click_appointment_list(self):
        """点击预约列表标签"""
        self.clickable_wait(self.appointment_list_locator).click()

    """查询功能"""
    def search_appointment(self,mobile):
        """通过手机号查询预约"""
        self.visible_only_wait(self.arrivalDate_All_locator).click()
        mobile_input = self.visible_only_wait(self.search_input_locator)
        mobile_input.clear()
        mobile_input.send_keys(mobile)
        self.clickable_wait(self.search_btn_locator).click()

    def search_assign(self):
        """查询待分配的预约"""
        self.clickable_wait(self.arrivalDate_All_locator).click()
        self.clickable_wait(self.appointmentStatus_assign_locator).click()

    def search_finished(self):
        """查询已完成的预约"""
        self.visible_only_wait(self.arrivalDate_All_locator).click()
        self.visible_only_wait(self.appointmentStatus_finished_locator).click()

    def search_cancel(self):
        """查询已取消的预约"""
        self.visible_only_wait(self.arrivalDate_All_locator).click()
        self.visible_only_wait(self.appointmentStatus_cancel_locator).click()


    def select_search(self):
        """通过预约来源下拉查询"""
        self.clickable_wait(self.appointmentSource_btn_locator).click()
        elem = self.clickable_wait(self.appointmentSource_select_locator)
        self.random_JSselect_click(elem)

    """列表按钮功能"""
    def click_list_newAppointment(self):
        """点击新增预约按钮"""
        time.sleep(2)
        self.clickable_wait(self.addAppointment_btn_locator).click()

    def clicK_list_cancleAppointment(self):
        """点击取消预约按钮"""
        self.clickable_wait(self.list_cancleAppointment_locator).click()

    def click_list_change(self):
        """点击修改按钮"""
        self.clickable_wait(self.list_change_locator).click()

    def click_list_billing(self):
        """点击开单按钮"""
        self.clickable_wait(self.list_billing_locator).click()

    def click_list_veiw(self):
        """点击查看按钮"""
        self.clickable_wait(self.list_veiw_locator).click()


    def list_change(self):
        """列表-修改预约"""
        # 点击修改按钮
        self.click_list_change()
        # 修改时间
        self.select_arrivalDate()
        self.select_arrivalTime()
        # 修改服务项目
        self.select_service()
        # 点击确定
        self.click_change_confirm()




    
